package ptu.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import ptu.entity.vo.RecordsVo;
import ptu.result.Result;
import ptu.service.RecordsService;

import java.util.List;

@Controller
@RequestMapping("records")
public class RecordsController {

    @Autowired
    private RecordsService recordsService;

    @GetMapping
    @ResponseBody
    public Result selectAll(){
        return recordsService.queryAllRecord();
    }

    /**
     * 借书
     * @param bookName
     * @param username
     * @return {@link String }
     */
    @GetMapping("borrow")
    @ResponseBody
    public Result borrowBook(@RequestParam("bookName") String bookName, @RequestParam("username") String username){
        return recordsService.borrowBook(bookName,username);
    }

    /**
     * 还书
     * @param bookName
     * @param username
     * @return {@link String }
     */
    @GetMapping("returnBook")
    @ResponseBody
    public Result returnBook(String bookName,String username){
        return  recordsService.returnBook(bookName,username);
    }

    /**
     * 某人的借阅记录
     * @param username
     * @param model
     * @return {@link String }
     */
    @GetMapping("queryByUsername")
    public String queryByUsername(String username, Model model){
        Result result = recordsService.queryByUsername(username);
        model.addAttribute("list",(List<RecordsVo>)result.getData());
        model.addAttribute("username",username);
        return "user/returnBook";

    }


}
